package com.demo.eshop.pdetail.request;

import com.demo.eshop.pdetail.entity.ProductInventory;
import com.demo.eshop.pdetail.service.product.ProductInventoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;

/**
 * @author: e20160504
 * @date: 2018/10/29
 * @desc 重新加载商品库存的缓存
 */
public class ProductInventoryCacheReloadRequest implements Request{

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private ProductInventoryService productInventoryService;
    private Integer productId;

    private boolean forceRefresh;

    public ProductInventoryCacheReloadRequest(ProductInventoryService productInventoryService, Integer productId,boolean forceRefresh) {
        this.productInventoryService = productInventoryService;
        this.productId = productId;
        this.forceRefresh = forceRefresh;
    }

    @Override
    public void process() {

        //从数据库中查询最近的商品库存数量
        ProductInventory inventory = productInventoryService.findProductInventory(productId);

        logger.info("===已查询到商品的库存数量，商品Id = {},商品库存= {} ",productId,inventory.getInventoryCnt());

        //将最新的商品库存数量，刷新到redis缓存中
        productInventoryService.setProductInventoryCache(inventory);

    }

    @Override
    public Integer getProductId() {
        return productId;
    }

    public boolean isForceRefresh() {
        return forceRefresh;
    }

    public void setForceRefresh(boolean forceRefresh) {
        this.forceRefresh = forceRefresh;
    }


}
